import path from 'path';
import LoginModel from '../models/login';
import { JWTKEY } from '../config/index';
// import jwt from 'jsonwebtoken';

let Login = {
    async loginRequest (ctx, next) {
        await LoginModel.post(ctx.request.body)
            .then(res => {
                let data = res.text;
                let userInfo = JSON.parse(data);
                ctx.session.loginData = userInfo;
                try {
                    // const token = jwt.sign({
                    //     data: userInfo.data.real_name,
                    // }, JWTKEY, { expiresIn: '1h' });
                    // console.log(token);
                    // ctx.cookies.set('xone-token', token);
                    ctx.body = {
                        code: userInfo.code,
                        info: userInfo.data,
                        // token,
                    };
                } catch (err) {
                    console.log(err);
                    throw new Error(err);
                }
            })
            .catch(err => {
                ctx.body = {
                    code: 500,
                    message: err,
                };
            });
        // return next();
    },
    async logout (ctx, nex) {
        ctx.session.loginData = null;
        ctx.redirect('/');
    },
};
export default Login;
